package archer.framework.client.service;

import archer.framework.protocol.result.ExecuteResult;
import archer.framework.protocol.result.ExecuteResultProcessor;
import archer.framework.utils.RemoteFileServerUtils;
import archer.framework.utils.RemoteInvokeUtils;
import archer.framework.utils.ServletUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 远程接口访问Service
 *
 * @author christ
 * @date 2015/10/10
 */
@Service
public class RemoteInvokeService extends ExecuteResultProcessor {

    /**
     * 获得当前请求线程对应的HttpServletRequest
     *
     * @return
     */
    public static HttpServletRequest getRequest() {

        return ServletUtils.getCurrentRequest();
    }

    /**
     * 获得当前请求线程对应的HttpServletResponse
     *
     * @return
     */
    public static HttpServletResponse getResponse() {

        return ServletUtils.getCurrentResponse();
    }


    //-------------------------------------file--------------------------------------

    /**
     * 上传文件
     *
     * @param url
     * @param file
     * @return
     */
    public ExecuteResult remoteUpload(String url, MultipartFile file) {

        return RemoteFileServerUtils.upload(url, file);
    }

    //-------------------------------------rest post--------------------------------------

    public ExecuteResult remotePost(String url) {

        return remotePost(url, null);
    }

    public ExecuteResult remotePost(String url, Map<String, Object> queryMap) {

        return remotePost(null, null, url, queryMap);
    }

    public ExecuteResult remotePost(HttpServletRequest req, HttpServletResponse resp, String url) {

        return remotePost(req, resp, url, null);
    }

    public ExecuteResult remotePost(HttpServletRequest req, HttpServletResponse resp, String url, Map<String, Object> queryMap) {

        ExecuteResult result = RemoteInvokeUtils.post(req, resp, url, queryMap);

        resultHandle(result);

        return result;
    }

    //-------------------------------------rest get--------------------------------------

    public ExecuteResult remoteGet(String url) {

        return remoteGet(url, null);
    }

    public ExecuteResult remoteGet(String url, Map<String, Object> queryMap) {

        return remoteGet(null, null, url, queryMap);
    }

    public ExecuteResult remoteGet(HttpServletRequest req, HttpServletResponse resp, String url) {

        return remoteGet(req, resp, url, null);
    }

    public ExecuteResult remoteGet(HttpServletRequest req, HttpServletResponse resp, String url, Map<String, Object> queryMap) {

        ExecuteResult result = RemoteInvokeUtils.get(req, resp, url, queryMap);

        resultHandle(result);

        return result;
    }


    //-------------------------------------rest put--------------------------------------

    public ExecuteResult remotePut(String url) {

        return remotePut(url, null);
    }

    public ExecuteResult remotePut(String url, Map<String, Object> queryMap) {

        return remotePut(null, null, url, queryMap);
    }

    public ExecuteResult remotePut(HttpServletRequest req, HttpServletResponse resp, String url) {

        return remotePut(req, resp, url, null);
    }

    public ExecuteResult remotePut(HttpServletRequest req, HttpServletResponse resp, String url, Map<String, Object> queryMap) {

        ExecuteResult result = RemoteInvokeUtils.put(req, resp, url, queryMap);

        resultHandle(result);

        return result;
    }


    //-------------------------------------rest delete--------------------------------------

    public ExecuteResult remoteDelete(String url) {

        return remoteDelete(url, null);
    }

    public ExecuteResult remoteDelete(String url, Map<String, Object> queryMap) {

        return remoteDelete(null, null, url, queryMap);
    }

    public ExecuteResult remoteDelete(HttpServletRequest req, HttpServletResponse resp, String url) {

        return remoteDelete(req, resp, url, null);
    }

    public ExecuteResult remoteDelete(HttpServletRequest req, HttpServletResponse resp, String url, Map<String, Object> queryMap) {

        ExecuteResult result = RemoteInvokeUtils.delete(req, resp, url, queryMap);

        resultHandle(result);

        return result;
    }

    /**
     * 接收到服务端返回的数据后后置处理
     *
     * @param result
     */
    public void resultHandle(ExecuteResult result) {

    }

}
